home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / WINDCOM.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  12KB  |  483 lines

  1. #include "pt.h"
  2. #include "ctype.h"
  3. #include "string.h"
  4. #include "io.h"
  5.  
  6. void pascal
  7. /* XTAG:doNewWindow */
  8. doNewWindow(fn, fullScreen)
  9.     int fn;
  10. {
  11.     extern unsigned char msgBuffer[];
  12.     extern unsigned char textBuffer[];
  13.     extern int scrRows, scrCols;
  14.     extern int menuLine;
  15.  
  16.     register unsigned char *fileName;
  17.     int n, row1, row2, col1, col2;
  18.  
  19.     if( fullScreen ) {
  20.         row1 = row2 = (menuLine > 0 ? 1 : 0);
  21.         col1 = col2 = 0;
  22.     } else if( getBox(0, 0, &row1, &col1, &row2, &col2, 0) == 2 )
  23.         goto cancelWindow;
  24.  
  25.     if( row1 == row2 && col1 == col2 ) {
  26.         row2 = scrRows-1;
  27.         if( menuLine < 0 )
  28.             --row2;
  29.         col2 = scrCols-1;
  30.     }
  31.     /* do not allow very small windows */
  32.     if( row2 < row1+2 || col2 < col1+10 ) {
  33.         msg("Size too small, no window created", 1);
  34.         return;
  35.     }
  36.     if( fn == FNEWWINDOW )
  37.         fileName = getFileName("File to load into the new window: ");
  38.     else    /* fn == FNEWSEL -- file name is the selection */
  39.         fileName = getSelection(&textBuffer[0]);
  40.     if( fileName == NULL ) {
  41. cancelWindow:
  42.         msg("Window create cancelled", 1);
  43.         return;
  44.     }
  45.     if( fileName[0] == '\0' ) {
  46.         strcpy(fileName, "UnNamed.a");
  47.         (void)makeName(fileName);
  48.     }
  49.     fileName = noWhiteSpace(fileName);
  50.     strcpy(textBuffer, fileName);
  51.     if( access(textBuffer, 0) == -1 ) {
  52.         sprintf(msgBuffer, "y to create «%s»: ", textBuffer);
  53.         fileName = getInput(msgBuffer, "y", 1);
  54.         if( fileName == NULL || tolower(*fileName) != 'y' )
  55.             goto cancelWindow;
  56.         n = creatls(textBuffer, 0);
  57.         if( n < 0 ) {
  58.             sprintf(msgBuffer, "Cannot create «%s»: ",
  59.                 textBuffer);
  60.             msg(msgBuffer, 3);
  61.             goto cancelWindow;
  62.         } else
  63.             closels(n);
  64.     }
  65.     createWindow(textBuffer, row1, col1, row2, col2, CRTOP, 0);
  66. }
  67.  
  68. unsigned char * pascal
  69. /* XTAG:noWhiteSpace */
  70. noWhiteSpace(fileName)
  71.     register unsigned char *fileName;
  72. {
  73.     register int n;
  74.  
  75.     if( fileName != NULL || fileName[0] == '\0' ) {
  76.         /* eliminate white space around fileName */
  77.  
  78.         /* first white space in the beginning */
  79.         while( isspace(*fileName) )
  80.             ++fileName;
  81.  
  82.         /* now white space at the end */
  83.         n = strlen(fileName) - 1;
  84.         while( isspace(fileName[n]) )
  85.             --n;
  86.         fileName[n+1] = '\0';
  87.     }
  88.     return fileName;
  89. }
  90.  
  91. void pascal
  92. /* XTAG:bottomFile */
  93. bottomFile(w)
  94.     register struct window *w;
  95. {
  96.     extern int debug;
  97.  
  98.     long cp;
  99.     int fid, j;
  100.     register int i;
  101.  
  102.     if( w == NULL )
  103.         return;
  104.     /* remember where we came from */
  105.     w->rowLastline = w->numTopline;
  106.     cp = w->posBotline;
  107.  
  108.     fid = w->fileId;    /* we'll use this a lot */
  109.  
  110.     /* find the last line of the file */
  111.     i = 0;
  112.     while( 1 ) {
  113.         j = 1;
  114.         cp = nextLine(fid, cp, &j);
  115.         /* if j==0, we could not go down a line */
  116.         /* so we are at the end */
  117.         if( j == 0 )
  118.             break;
  119.         ++i;
  120.     }
  121.     ++i;    /* one more line so EOF mark shows */
  122.  
  123.     /* now move the window down and redraw it */
  124.     j = i;    /* since i is a register variable, we must use j here */
  125.     w->posTopline = nextLine(fid, w->posTopline, &j);
  126.     w->posBotline = cp;
  127.     w->numTopline += j;
  128.     w->numBotline += j;
  129.     w->indent = 0;
  130.     redrawWindow(w);
  131. }
  132.  
  133. void pascal
  134. /* XTAG:loadWindow */
  135. loadWindow(w, fn)
  136.     register struct window *w;
  137.     int fn;
  138. {
  139.     extern struct window *selWindow;
  140.     extern long selBegin, selEnd;
  141.     extern unsigned char msgBuffer[];
  142.     extern unsigned char textBuffer[];
  143.     extern struct openFile *files;
  144.  
  145.     int n;
  146.     unsigned char *fileName, saveName[64];
  147.  
  148.     if( fn == FLOADFILE )
  149.         fileName = getFileName("File to load in window: ");
  150.     else    /* fn == FLOADSEL -- get file name from the selection */
  151.         fileName = getSelection(&textBuffer[0]);
  152.     if( fileName == NULL )
  153.         goto cancelFileLoad;
  154.     if( fileName[0] == '\0' ) {
  155.         strcpy(fileName, "UnNamed.a");
  156.         makeName(fileName);
  157.     }
  158.     fileName = noWhiteSpace(fileName);
  159.     strcpy(saveName, fileName);
  160.     n = 0;    /* we did NOT create 'saveName' */
  161.     if( access(saveName, 0) == -1 ) {
  162.         sprintf(msgBuffer, "y to create «%s»: ", saveName);
  163.         fileName = getInput(msgBuffer, "y", 1);
  164.         if( fileName == NULL || tolower(*fileName) != 'y' )
  165.             goto cancelFileLoad;
  166.         n = creatls(saveName, 0);
  167.         if( n < 0 ) {
  168.             sprintf(msgBuffer, "Cannot create «%s»: ", saveName);
  169.             msg(msgBuffer, 3);
  170.             goto cancelFileLoad;
  171.         } else
  172.             closels(n);
  173.         n = 1;    /* we did create 'saveName' */
  174.     }
  175.     if( closeFile(w->fileId, 1) == -1 ) {
  176.         if( n )    /* delete 'saveName' if we created it */
  177.             delete(saveName);
  178. cancelFileLoad:
  179.         msg("File load cancelled", 1);
  180.         return;
  181.     }
  182.     w->fileId = getFileId(saveName);
  183.     w->nameOffset = getBaseName(files[w->fileId].origName);
  184.     w->posTopline = 0;
  185.     w->numTopline = 1;
  186.     w->posCurLast = 0;
  187.     w->lastPosTop = 0;
  188.     w->rowCurLast = 0;
  189.     w->indent = 0;
  190.     if( w == selWindow ) {
  191.         selBegin = selEnd = 0;
  192.         if( readChar(w->fileId, 0L) == '\r' ) {
  193.             if( readChar(w->fileId, 1L) == '\n' )
  194.                 selEnd = 1;
  195.         }
  196.     }
  197.     /* get the state of unixMode */
  198.     w->state = getUnixState(w->fileId);
  199.     redrawWindow(w);
  200. }
  201.  
  202. void pascal
  203. /* XTAG:hideWindow */
  204. hideWindow(w)
  205.     register struct window *w;
  206. {
  207.     extern struct window *windowList;
  208.     extern struct window *activeWindow;
  209.     extern struct window *hiddenList;
  210.     extern struct window *selWindow;
  211.     extern long selBegin, selEnd;
  212.     extern int scrRows, scrCols;
  213.     
  214.     if( w == NULL )
  215.         return;
  216.  
  217.     /* unlink the window from the list of active windows */
  218.     if( w->prevWindow != NULL )
  219.         w->prevWindow->nextWindow = w->nextWindow;
  220.     else    /* must be first on the list -- the top window */
  221.         windowList = w->nextWindow;
  222.     if( w->nextWindow != NULL )
  223.         w->nextWindow->prevWindow = w->prevWindow;
  224.  
  225.     /* stack w onto the hidden list */
  226.     w->nextWindow = hiddenList;
  227.     hiddenList = w;
  228.  
  229.     /* is the selection in this window? */
  230.     if( w == selWindow ) {
  231.         /* move the selection to the top window */
  232.         selWindow = windowList;
  233.         if( selWindow != NULL ) {
  234.             selBegin = 0;
  235.             selEnd = 0;
  236.             if( readChar(selWindow->fileId, 0L) == '\r' ) {
  237.                 if( readChar(selWindow->fileId, 1L) == '\n' )
  238.                     selEnd = 1;
  239.             }
  240.         }
  241.     }
  242.  
  243.     /* is this the active window? */
  244.     if( w == activeWindow )
  245.         activeWindow = windowList;
  246.  
  247.     /* redraw the screen */
  248.     redrawBox(0, 0, scrRows-1, scrCols-1);
  249.     updateScreen(0, scrRows-1);
  250. }
  251.  
  252. void pascal
  253. /* XTAG:doContextMenu */
  254. doContextMenu()
  255. {
  256.     extern struct window *windowList;
  257.     extern struct window *hiddenList;
  258.     extern int menuRow, menuCol;
  259.     extern struct menuBlock far *menus[];
  260.     extern struct openFile *files;
  261.     extern int pathNames;
  262.     
  263.     int i, n, k;
  264.     struct window *newW;
  265.     register struct window *w2;
  266.  
  267.     /* put in the title of the menu */
  268.     menus[0]->cmdName[0] = (unsigned char far *)"TOPLIST";
  269.     menus[0]->cmdNumber[0] = 0;
  270.  
  271.     /* go through the list of visible windows */
  272.     n = 1;
  273.     i = 1;
  274.     w2 = windowList;
  275.     while( w2 != NULL && n < MAXMENUITEMS ) {
  276.         k = (pathNames ? 0 : w2->nameOffset);
  277.         menus[0]->cmdName[n] = (unsigned char far *)
  278.                     &(files[w2->fileId].origName[k]);
  279.         menus[0]->cmdNumber[n] = i;
  280.         w2 = w2->nextWindow;
  281.         ++n;
  282.         ++i;
  283.     }
  284.  
  285.     if( hiddenList != NULL && n < MAXMENUITEMS ) {
  286.         /* put in a divider line */
  287.         menus[0]->cmdName[n] = (unsigned char far *)"════════════";
  288.         menus[0]->cmdNumber[n] = 0;
  289.         ++n;
  290.     }
  291.  
  292.     /* now go through the list of hidden windows */
  293.     w2 = hiddenList;
  294.     /* use "command" numbers in the 300's to indicate hidden */
  295.     /* windows.  This avoids conflicts with the real command */
  296.     /* number from 1 to ~150.  For ~100 to ~150 they are option */
  297.     /* setting commands and so they ignore the string you send in. */
  298.     i = 301;    /* number the entries */
  299.     while( w2 != NULL && n < MAXMENUITEMS ) {
  300.         k = (pathNames ? 0 : w2->nameOffset);
  301.         menus[0]->cmdName[n] =
  302.             (unsigned char far *)&(files[w2->fileId].origName[k]);
  303.         menus[0]->cmdNumber[n] = i;
  304.         w2 = w2->nextWindow;
  305.         ++n;
  306.         ++i;
  307.     }
  308.  
  309.     /* now display the menu */
  310.     menus[0]->nItems = n;
  311.     i = menu(menus[0], menuRow, menuCol);
  312.  
  313.     /* find out which window to top (or unhide and top) */
  314.     if( i == 0 )
  315.         return;
  316.     if( i > 300 ) {    /* unhide and top a hidden window */
  317.         i -= 301;
  318.         if( i == 0 ) {
  319.             newW = hiddenList;
  320.             hiddenList = hiddenList->nextWindow;
  321.         } else {
  322.             w2 = hiddenList;
  323.             while( --i > 0 )
  324.                 w2 = w2->nextWindow;
  325.             newW = w2->nextWindow;
  326.             /* unlink